<template>
  <j-modal
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item label="疾病名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['diseaseName',{rules: [{required: true,message: '请输入疾病名称!',}]}]"
                   placeholder="请输入疾病名称"></a-input>
        </a-form-item>
        <a-form-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input v-decorator="['sort',{rules: [{required: true,message: '请输入排序!',}]}]"
                   placeholder="请输入疾病排序"></a-input>
        </a-form-item>
        <!--<a-form-item label="所属科室" :labelCol="labelCol" :wrapperCol="wrapperCol">-->
        <!--<a-select v-decorator="['departmentId',{rules: [{required: true,message: '请选择所属医院!',}]}]"-->
        <!--placeholder="请选择所属科室">-->
        <!--<a-select-option v-for="d in departmentList" :key="d.id" :value="d.id">{{ d.departmentName }}-->
        <!--</a-select-option>-->
        <!--</a-select>-->
        <!--</a-form-item>-->
        <a-form-item label="关联科室" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            mode="multiple"
            v-model="departmentIdList"
            style="width: 100%"
            placeholder="关联科室"
            @change="handleChange2" show-search option-filter-prop="children" :filter-option="filterOption2">
            <a-select-option v-for="item in departmentList" :value="item.id" :key="item.id">
              {{ item.departmentName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="是否为常见疾病" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-switch checked-children="是" un-checked-children="否" default-checked @change="switchOn" v-model="checked"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import {getAction, httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import {validateDuplicateValue} from '@/utils/util'
  import JDate from '@/components/jeecg/JDate'


  export default {
    name: "MedicalDiseaseInfoModal",
    components: {
      JDate,
    },
    data() {
      return {
        form: this.$form.createForm(this),
        title: "操作",
        width: 800,
        visible: false,
        checked: true,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },
        confirmLoading: false,
        validatorRules: {},
        url: {
          add: "/hospital/medicalDiseaseInfo/add",
          edit: "/hospital/medicalDiseaseInfo/edit",
          getDepartmentList: "/hospital/medicalDepartmentInfo/getNodeDepartment",
          getSymptomList: "/sms/smsBodyPartsSymptom/getList",
        },
        departmentList: [],
        symptomList: [],
        symptomIdList: [],
        departmentIdList: [],
      }
    },
    created() {
    },
    mounted() {
      this.initIndex();
    },
    methods: {
      initIndex() {
        getAction(this.url.getDepartmentList).then((res) => {
          if (res.success) {
            this.departmentList = res.result;
          }
        });
        // getAction(this.url.getSymptomList).then((res) => {
        //   if (res.success) {
        //     this.symptomList = res.result;
        //   }
        // });
      },
      add() {
        this.edit({});
      },
      edit(record) {
        this.symptomIdList = []
        this.departmentIdList = []
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        if (record.id) {
          this.checked = record.commonStatus == 0 ? false : true;
          // this.symptomIdList = record.symptomIdList;
          this.departmentIdList = record.departmentIdList;
        } else {
          //设置默认展示
          this.model.commonStatus = 1;
        }
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'diseaseName', 'departmentId', 'commonStatus', 'sort'))
        })
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            // formData.symptomIdList = this.symptomIdList
            formData.departmentIdList = this.departmentIdList
            console.log("表单提交数据", formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })
          }

        })
      },
      handleCancel() {
        this.close()
      },
      popupCallback(row) {
        this.form.setFieldsValue(pick(row, 'diseaseName', 'departmentId', 'commonStatus'))
      },
      switchOn(checked) {
        if (checked) {
          this.model.commonStatus = 1;
          this.checked = true;
        } else {
          this.model.commonStatus = 0;
          this.checked = false;
        }
      },
      handleChange(value) {
        this.symptomIdList = value;
      },
      filterOption(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
      handleChange2(value) {
        this.departmentIdList = value;
      },
      filterOption2(input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        );
      },
    }
  }
</script>